package com.gz.logistics.library.listener;

import com.gz.logistics.library.pojo.WillOutStorageAassembleY;
import com.gz.logistics.library.util.Constants;
import com.gz.logistics.library.util.CrashHandler;

import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

/**
 * 预出库已签收列表监听
 */
public abstract class GetWillOutOfStorageAssemblesListenerY extends BaseListener {
    private String TAG = GetWillOutOfStorageAssemblesListenerY.class.getSimpleName();

    public GetWillOutOfStorageAssemblesListenerY(RequestCode requestCode) {
        super(requestCode);
    }

    protected GetWillOutOfStorageAssemblesListenerY() {
    }
    @Override
    public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
        try {
            int status = response.getInt(Constants.REQ_STATUS);
            String msg = response.getString(Constants.REQ_MSG);
            switch(status){
                case Constants.REQ_OK:  //在加载第二页的时候，如果数据为空，返回的ret还是0（和订单查询界面不同）
                    JSONArray jsonArray = response.getJSONArray(Constants.REQ_RESULT);
                    onResponseSuccess(WillOutStorageAassembleY.parse(jsonArray));
                    break;
                case Constants.REQ_NO_ASSEMBLES:
                    onNoConditionAssembles();
                    break;
                case Constants.REQ_NO_SPECIFIC_THIS_ORDER:
                    onNoMoreData();
                    break;
                default:
                    onResponseFailed();
                    break;
            }

        } catch (JSONException e) {
            onFailure(statusCode, headers, e, new JSONObject());
        }
    }

    /**
     * 没有满足条件的预出库单
     */
    public abstract void onNoConditionAssembles();
    public abstract void onResponseSuccess(List<WillOutStorageAassembleY> assembles);
    @Override
    public void onFailure(int statusCode,
                          Header[] headers,
                          Throwable throwable,
                          JSONObject errorResponse) {
        CrashHandler.e(throwable);
        CrashHandler.i(TAG,"onFailure"+"发生异常");
        onResponseFailed();
    }

    @Override
    public void onOffline() {

    }

}
